package cc.mrbird.febs.item.service.impl;

import cc.mrbird.febs.common.domain.QueryRequest;
import cc.mrbird.febs.common.utils.SortUtil;
import cc.mrbird.febs.item.dao.ItemMapper;
import cc.mrbird.febs.item.domain.ItemBean;
import cc.mrbird.febs.item.service.ItemService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;

@Service("ItemService")
public class ItemServiceImpl extends ServiceImpl<ItemMapper, ItemBean> implements ItemService {
    @Override
    public IPage<ItemBean> listItemById(ItemBean itemBean, QueryRequest queryRequest) {
        try {
            Page<ItemBean> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page);
            return this.baseMapper.listItemById(page, itemBean);
        } catch (Exception e) {
            log.error("查询异常", e);
            return null;
        }
    }

    @Override
    public IPage<ItemBean> listItemByUser(ItemBean itemBean, QueryRequest queryRequest) {
        try {
            Page<ItemBean> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page);
            return this.baseMapper.listItemByUser(page, itemBean);
        } catch (Exception e) {
            log.error("查询异常", e);
            return null;
        }
    }

    @Override
    public IPage<ItemBean> listItemByName(ItemBean itemBean, QueryRequest queryRequest) {
        try {
            Page<ItemBean> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page);
            if(itemBean.getItemName() == null) {
                itemBean.setItemName("");
            }
            return this.baseMapper.listItemByName(page, itemBean);
        } catch (Exception e) {
            log.error("查询异常", e);
            return null;
        }
    }

    @Override
    public List<ItemBean> listItem() {
        return this.baseMapper.listItem();
    }

    @Override
    public IPage<ItemBean> listLikeItem(ItemBean itemBean, QueryRequest queryRequest) {
        try {
            Page<ItemBean> page = new Page<>();
            SortUtil.handlePageSort(queryRequest, page);
            return this.baseMapper.listLikeItem(page, itemBean);
        } catch (Exception e) {
            log.error("查询异常", e);
            return null;
        }
    }

    @Override
    public void saveRate(ItemBean itemBean) {
        if (baseMapper.countRate(itemBean) > 0) {
            baseMapper.updateRate(itemBean);
        } else {
            baseMapper.insertRate(itemBean);
        }
    }

}
